package com.xiaozhengkeji.springboot.speed.services.alipay.imp;

import com.alibaba.fastjson.JSONArray;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.xiaozhengkeji.springboot.speed.dao.OrderListDao;
import com.xiaozhengkeji.springboot.speed.mock.AliPayTradePrecreateMock;
import com.xiaozhengkeji.springboot.speed.pojo.OrderListPoJo;
import com.xiaozhengkeji.springboot.speed.services.UserService;
import com.xiaozhengkeji.springboot.speed.services.alipay.AliPayService;
import com.xiaozhengkeji.springboot.speed.utils.AlipayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings("ALL")
@Slf4j
@Service
public class AliPayServiceImp implements AliPayService {
    @Autowired
    private OrderListDao orderListDao;
    @Autowired
    protected UserService userService;


    @Override
    public String 新建订单(String 订单备注, String 订单号, Double 订单金额, String 订单标题) {
        //创建API对应的request类
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AliPayTradePrecreateMock aliPayTradePrecreateEntity = new AliPayTradePrecreateMock(订单号, String.valueOf(订单金额), 订单标题, "A0", "90m");
        request.setBizContent(JSONArray.toJSONString(aliPayTradePrecreateEntity));
        try {
            AlipayTradePrecreateResponse response = AlipayUtils.getAlipayClient().execute(request);
            OrderListPoJo orderListPoJo = new OrderListPoJo();
            orderListPoJo.setAmountss(订单金额);
            orderListPoJo.setOrdersss(订单号);
            orderListPoJo.setStatesss("等待支付!");
            orderListPoJo.setNotesss(订单备注);
            orderListDao.insert(orderListPoJo);
            return response.getQrCode();
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return "服务器内部错误!";
        }
    }

    @Override
    public Boolean 查询订单(String 订单号) {
        //创建API对应的request类
        AlipayTradeQueryRequest request0 = new AlipayTradeQueryRequest();
        //trade_no
        request0.setBizContent("{" +
                "    \"out_trade_no\":\"" + 订单号 + "\"}");
        //通过alipayClient调用API，获得对应的response类
        AlipayTradeQueryResponse response = null;
        try {
            response = AlipayUtils.getAlipayClient().execute(request0);
            String 返回代码 = response.getCode();
            if (返回代码.equalsIgnoreCase("10000")) {
                return true;
            } else {
                return false;
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return false;
        }

    }
}
